# Build recipe for ruby.
#
# Copyright (c) 2019, 2021-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=ruby
version=3.0.3
pkgname=ruby3
release=1

# Define a category for the output of the package name
pkgcategory=ruby

tarname=${program}-${version}.tar.gz

# Remote source(s)
fetch="https://cache.ruby-lang.org/pub/ruby/${version%.*}/$tarname"

description="
Ruby programming language.

Ruby is a dynamic, open source programming language with a focus
on simplicity and productivity.  It has an elegant syntax that is
natural to read and easy to write.
"

homepage=https://www.ruby-lang.org
license="GPLv2+ | BSD + Custom declared files (see LEGAL)"

# Source documentation
docs="
  BSDL CONTRIBUTING.md COPYING* ChangeLog GPL KNOWNBUGS.rb
  LEGAL NEWS.md README*
"
docsdir="${docdir}/${pkgname}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Apply patches from "Alpine Linux"
    patch -Np1 -i "${worktree}/patches/ruby/test_insns-lower-recursion-depth.patch"
    patch -Np1 -i "${worktree}/patches/ruby/fix-get_main_stack.patch"
    patch -Np1 -i "${worktree}/patches/ruby/dont-install-bundled-gems.patch"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure \
    CPPFLAGS="$QICPPFLAGS -D_GNU_SOURCE -fno-omit-frame-pointer -fno-strict-aliasing" \
    CFLAGS="$QICFLAGS -fno-omit-frame-pointer -fno-strict-aliasing" \
    CXXFLAGS="$QICXXFLAGS -fno-omit-frame-pointer -fno-strict-aliasing" \
    LDFLAGS="$QILDFLAGS" INSTALL="install" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --docdir=$docsdir \
     --enable-shared \
     --enable-ipv6 \
     --enable-load-relative \
     --with-mantype=man \
     --with-sitedir=/usr/local/share/ruby \
     --with-sitearchdir=/usr/local/lib${libSuffix}/ruby \
     --without-compress-debug-sections \
     --disable-rpath \
     --build="$(gcc -dumpmachine)" \
     ac_cv_func_isnan=yes \
     ac_cv_func_isinf=yes

    make -j${jobs} V=1 optflags="-O3"
    make -j${jobs} DESTDIR="$destdir" install

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

